﻿using System;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace YArchitech.MEP
{
	public abstract class HYComponent : IXmlSerializable
	{
		public int Id { get; set; }

		public virtual string Name
		{
			get
			{
				return this.m_Name;
			}
			set
			{
				this.m_Name = value;
			}
		}

		public HYBranchPlace Place { get; set; }

		public int Index { get; set; }

		public HYTubeSize Size
		{
			get
			{
				return this.m_Size;
			}
			set
			{
				this.m_Size = value;
			}
		}

		public double Flow { get; set; }

		public virtual string GetShowString()
		{
			return this.Name + this.Index.ToString();
		}

		public HYComponent Previous
		{
			get
			{
				return this.m_Previous;
			}
			set
			{
				this.m_Previous = value;
			}
		}

		public HYComponent Next
		{
			get
			{
				return this.m_Next;
			}
		}

		public HYComponent Side1
		{
			get
			{
				return this.m_Side1;
			}
		}

		public HYComponent Side2
		{
			get
			{
				return this.m_Side2;
			}
		}

		public bool IsTerminal
		{
			get
			{
				return this.m_Next == null;
			}
		}

		public static HYBranchPlace Up(HYBranchPlace p)
		{
			if (HYBranchPlace.SideSide == p)
			{
				return HYBranchPlace.Side;
			}
			return HYBranchPlace.Main;
		}

		public static HYBranchPlace Down(HYBranchPlace p)
		{
			if (p == HYBranchPlace.Main)
			{
				return HYBranchPlace.Side;
			}
			return HYBranchPlace.SideSide;
		}

		public void SetNext(HYComponent next)
		{
			this.m_Next = next;
			if (next != null)
			{
				next.m_Previous = this;
				next.Place = this.Place;
			}
		}

		public void SetSide(HYComponent side, int index = 0)
		{
			if (index != 0)
			{
				if (index == 1)
				{
					this.m_Side2 = side;
				}
			}
			else
			{
				this.m_Side1 = side;
			}
			if (side != null)
			{
				side.m_Previous = this;
				side.Place = HYComponent.Down(this.Place);
			}
		}

		public virtual void AddToSystem(HYSystem hySystem)
		{
			hySystem.Add(this);
			if (this.m_Next != null)
			{
				this.m_Next.AddToSystem(hySystem);
			}
			if (this.m_Side1 != null)
			{
				this.m_Side1.AddToSystem(hySystem);
			}
			if (this.m_Side2 != null)
			{
				this.m_Side2.AddToSystem(hySystem);
			}
		}

		public virtual void RebuildIndex(ref int index)
		{
			if (this.m_Next != null)
			{
				this.m_Next.RebuildIndex(ref index);
			}
			if (this.m_Side1 != null)
			{
				this.m_Side1.RebuildIndex(ref index);
			}
			if (this.m_Side2 != null)
			{
				this.m_Side2.RebuildIndex(ref index);
			}
		}

		public virtual void ReAllocateFlow()
		{
			double num = 0.0;
			if (this.m_Next != null)
			{
				this.m_Next.ReAllocateFlow();
				num = this.m_Next.Flow;
			}
			if (this.m_Side1 != null)
			{
				this.m_Side1.ReAllocateFlow();
				num += this.m_Side1.Flow;
			}
			if (this.m_Side2 != null)
			{
				this.m_Side2.ReAllocateFlow();
				num += this.m_Side2.Flow;
			}
			if (num > this.Flow)
			{
				this.Flow = num;
			}
		}

		public virtual void AllocateFlow()
		{
			double num = 0.0;
			if (this.m_Next != null)
			{
				this.m_Next.AllocateFlow();
				num = this.m_Next.Flow;
			}
			if (this.m_Side1 != null)
			{
				this.m_Side1.AllocateFlow();
				num += this.m_Side1.Flow;
			}
			if (this.m_Side2 != null)
			{
				this.m_Side2.AllocateFlow();
				num += this.m_Side2.Flow;
			}
			this.Flow = num;
		}

		public virtual void Cal(HYSystemOption op)
		{
		}

		public XmlSchema GetSchema()
		{
			return null;
		}

		public virtual void ReadXml(XmlReader reader)
		{
		}

		public void WriteXml(XmlWriter writer)
		{
		}

		protected string m_Name;

		protected HYTubeSize m_Size;

		protected HYComponent m_Previous;

		protected HYComponent m_Next;

		protected HYComponent m_Side1;

		protected HYComponent m_Side2;
	}
}
